(function($) {
$.fn.extend({
	divSelect:function(options){   
		var opts = $.extend({
			callback:function(){}
		},options);
		var obj = $(this);
		$(this).attr("readonly","readonly");//设置控件只读
		var top = $(this).offset().top;//获取控件top、left位置和width、height 
		var left = $(this).offset().left; 
		var height = $(this).height(); 
		var width = $(this).width()+3; 
		var option_open = false;//标记是否打开下拉option
		var divSelectID = $(this).attr("id");
		var noselectCss = "cursor: pointer;background: #f4f4f4 right url(/images/select_arrow_over.jpg) no-repeat;width:88px;";
		var selectCss = "cursor: pointer;background: #f4f4f4;";
		$(this).attr("style", noselectCss);
		
		function __dropheight(l){var h;if(l>10 || l<1){h = 10 * 20;}else{h = l * 20; h += 2;}return h;}  //计算下拉option显示高度 
		//var div_html = "<div id='_divSelect_"+divSelectID+"' style='position:absolute;background-color:#FFFFFF;top:"+(top+height+5)+"px;left:"+left+"px;width:auto;height:auto;border:1px #666666 solid;over-flow:hidden;display:none;z-index:99999;'><div style='text-align:left;padding-left:5px;cursor: pointer;width:auto;display:block;' sv=''>Cancel Select</div>";//"+((width<30)?30:width)+"px, "+__dropheight(opts.data.length)+"px 
		var div_html = "<div class='_divSelect_vm_' id='_divSelect_"+divSelectID+"' style='position:absolute;background-color:#FFFFFF;top:"+(top+height+5)+"px;left:"+left+"px;width:auto;height:auto;border:1px #666666 solid;over-flow:hidden;display:none;z-index:99999;'><div style='text-align:left;padding-left:5px;cursor: pointer;width:auto;display:block;' sv=''>Cancel Select</div>";
		//for循环填充option 
	 	for(var i = 0;i < opts.data.length; i++){
	 		var display = '';
	 		if (opts.data[i]['display'] != undefined) {
	 			display = opts.data[i]['display'];
	 		} else {
	 			display = opts.data[i]['key'];
	 		}
	 		if(opts.data[i]['img'] != undefined) {
				//div_html += "<div style='text-align:left;padding:2px 0 2px 5px;cursor: pointer;width: 345px;over-flow:hidden;float:left;display:inline;' sv='" + opts.data[i]['key'] + "'><span style='float:left;display:inline;width: auto;height: 27px;'><img src='"+opts.data[i]['img']+"' height='27' /></span><span style='float:left;display:inline;width: auto;height: 27px;line-height: 27px;padding-left: 6px;'>" + opts.data[i]['key'] + "</span></div>";
	 			div_html += "<div style='text-align:left;padding:2px 0 2px 5px;cursor: pointer;width: auto;display:block;' sv='" + opts.data[i]['key'] + "'>";
	 			var tmpArr = opts.data[i]['img'].split('|');
	 			for (var item in tmpArr) {
	 				div_html += "<span style='width: auto;height: 27px;'><img src='/"+tmpArr[item]+"' height='27' /></span>";
	 			}
	 			div_html += "<span style='width: auto;height: 27px;line-height: 27px;padding-left: 6px;'>" + display + "</span></div>";
			} else {
				div_html += "<div style='text-align:left;padding:2px 0 2px 5px;cursor: pointer;width: auto;display:block;' sv='" + opts.data[i]['key'] + "'>" + display + "</div>";
			}
	 	} 
		div_html += "</div>";
		//$(this).after(div_html);//添加到input控件后面
		$(document.body).append(div_html);
		//function __open_option(){$("div[id^='_divSelect_']").hide();$("#_divSelect_"+divSelectID).show();option_open=true;}  //显示下拉option 
		function __open_option(){top = obj.offset().top;left = obj.offset().left;$("div[id^='_divSelect_']").css("top",top).css("left",left);$("div[id^='_divSelect_']").hide();$("#_divSelect_"+divSelectID).show();option_open=true;}
		function __hide_option(){$("#_divSelect_"+divSelectID).hide();option_open=false;$(document).unbind("click",__hide_option);}  //隐藏下拉option 
		
		$(this).click(
			function(event){
				if(option_open){
					__hide_option();
				}else{
					__open_option();
					$(document).bind("click",__hide_option);
				}
			event.stopPropagation();
		}); 
		
		$("#_divSelect_" + divSelectID + " > div").hover(
			function(){
				$(this).css({"background-color":"#CCCCCC"});
				$(this).find('img').each(function(){$(this).attr("height",60)});
			},
			function(){
				$(this).css({"background-color":"#FFFFFF"});
				$(this).find('img').each(function(){$(this).attr("height",27)});
			}
		).click(function(){
			$("#"+divSelectID).val($(this).attr("sv"));
			
			if($(this).attr("sv") != "")
			{
				$("#" + divSelectID).attr("style", selectCss);
			} else {
				$("#" + divSelectID).attr("style", noselectCss);
			}
			//
			__hide_option();
			//执行后回调函数
			opts.callback($("#"+divSelectID), $(this).attr("sv"));
		});
		/*
		$("#_divSelect_" + divSelectID + " > div").find('img').hover(
			function(){ $(this).attr('height',60); },
			function(){ $(this).attr('height',27); }
		);*/

		return this.each(function(){   
			//
		});
	}
});

})(jQuery);